package com.qdu.study_app.Widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.qdu.study_app.R;

/*
* 写圆形的进度条
* */
public class ClockProgressBar extends View {
    private int minnerBackground;
    private int moutterBackground;
    private int mroundWidth = 10; // 10dp
    private float mprogressTextSize = 18; // 18sp
    private int mprogressTextColor;
    private long mMax = 1000; // 圆弧的最大进程
    private long mProgress = 0;

    private Paint mInnerpaint; // 内圆画笔
    private Paint mOutterpaint; // 外圆画笔

// 生成构造函数
    public ClockProgressBar(Context context) {
        this(context,null);
    }

    public ClockProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ClockProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ClockProgressBar);
        minnerBackground = array.getColor(R.styleable.ClockProgressBar_innerBackground,minnerBackground);
        moutterBackground = array.getColor(R.styleable.ClockProgressBar_outterBackground,moutterBackground);
        mroundWidth =(int)array.getDimension(R.styleable.ClockProgressBar_roundWidth,dipToPx(10));
        mprogressTextColor = array.getColor(R.styleable.ClockProgressBar_progressTextColor,mprogressTextColor);
        mprogressTextSize = (int)array.getDimension(R.styleable.ClockProgressBar_progressTextSize,spToPx(mprogressTextSize));
        array.recycle();
        //初始化内圆画笔
        mInnerpaint = new Paint();
        mInnerpaint.setAntiAlias(true);
        mInnerpaint.setColor(minnerBackground); // 设置画笔的颜色
        mInnerpaint.setStrokeWidth(mroundWidth);// 设置画笔的粗细
        mInnerpaint.setStyle(Paint.Style.STROKE);//
        // 初始化外圆画笔
        mOutterpaint = new Paint();
        mOutterpaint.setAntiAlias(true);
        mOutterpaint.setColor(moutterBackground);
        mOutterpaint.setStrokeWidth(mroundWidth);
        mOutterpaint.setStyle(Paint.Style.STROKE);
    }

    /*
    * 像素转换方法
    * */
    private  float dipToPx(int dip){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics());
    }
    private  float spToPx(float sp){
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp,getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 只保证是正方形
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(Math.min(width,height),Math.min(width,height));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 先画内圆
        int center = getWidth()/2; // 确定圆心
        // 开始画圆
        canvas.drawCircle(center,center,center-mroundWidth/2,mInnerpaint);
        // 画外圆,画圆弧.画笔的宽度是一个小方块，笔芯是中心点
        RectF rect = new RectF(0+mroundWidth/2,0+mroundWidth/2,
                getWidth()-mroundWidth/2,getHeight()-mroundWidth/2);
        if(mProgress == 0){ return; } // 一开始进入时会画一次
        float percent = (float) mProgress/mMax;
        // 开始的角度是-90度
        canvas.drawArc(rect,-90,percent * 360,false,mOutterpaint);

        // 画进度文字
    }
    public synchronized void setMaxStep(long maxStep){
        if(maxStep < 1000){
            throw new IllegalArgumentException("总时间不能小于1s");
        }
        this.mMax = maxStep;
    }
    public synchronized void setProgress(long progress){
        if(progress < 0){
            throw new IllegalArgumentException("progress不能小于0");
        }
        this.mProgress = progress;
        invalidate();// 重新绘制
    }
    public synchronized long getProgress(){
        return this.mProgress;
    }
    public synchronized long getMax(){
        return this.mMax;
    }
}
